<template>
  <div class="green-apply">
    <div class="header">
      <router-link to="/main/huanjing/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>绿化养护申请</h2>
    </div>

    <div class="content">
      <el-form :model="formData" ref="form" :rules="rules" label-width="120px" class="application-form">
        <el-form-item label="申请编号">
          <el-input v-model="formData.applyNo" disabled></el-input>
        </el-form-item>

        <el-form-item label="申请人" prop="applicant">
          <el-input v-model="formData.applicant"></el-input>
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone"></el-input>
        </el-form-item>

        <el-form-item label="养护区域" prop="location">
          <el-cascader
            v-model="formData.location"
            :options="locationOptions"
            :props="{ expandTrigger: 'hover' }"
            placeholder="请选择养护区域">
          </el-cascader>
        </el-form-item>

        <el-form-item label="养护类型" prop="greenType">
          <el-select v-model="formData.greenType" placeholder="请选择养护类型">
            <el-option label="日常养护" value="日常养护"></el-option>
            <el-option label="修剪整形" value="修剪整形"></el-option>
            <el-option label="病虫害防治" value="病虫害防治"></el-option>
            <el-option label="补种更换" value="补种更换"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="期望养护时间" prop="expectedTime">
          <el-date-picker
            v-model="formData.expectedTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="养护要求" prop="requirements">
          <el-input 
            type="textarea" 
            v-model="formData.requirements" 
            rows="4"
            placeholder="请详细描述养护要求">
          </el-input>
        </el-form-item>

        <el-form-item label="备注">
          <el-input type="textarea" v-model="formData.remarks" rows="3"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="submitting">提交申请</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'GreenApply',
  data() {
    return {
      submitting: false,
      formData: {
        applyNo: this.generateApplyNo(),
        applicant: '',
        phone: '',
        location: [],
        greenType: '',
        expectedTime: [],
        requirements: '',
        remarks: ''
      },
      locationOptions: [
        {
          value: '室外',
          label: '室外',
          children: [
            {
              value: '花园',
              label: '花园',
              children: [
                { value: '东区', label: '东区' },
                { value: '西区', label: '西区' },
                { value: '中心区', label: '中心区' }
              ]
            },
            {
              value: '绿化带',
              label: '绿化带',
              children: [
                { value: '主干道', label: '主干道' },
                { value: '次干道', label: '次干道' }
              ]
            }
          ]
        },
        {
          value: '室内',
          label: '室内',
          children: [
            {
              value: '办公区',
              label: '办公区',
              children: [
                { value: '大厅', label: '大厅' },
                { value: '走廊', label: '走廊' }
              ]
            }
          ]
        }
      ],
      rules: {
        applicant: [
          { required: true, message: '请输入申请人', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请选择养护区域', trigger: 'change' }
        ],
        greenType: [
          { required: true, message: '请选择养护类型', trigger: 'change' }
        ],
        expectedTime: [
          { required: true, message: '请选择期望养护时间', trigger: 'change' }
        ],
        requirements: [
          { required: true, message: '请输入养护要求', trigger: 'blur' },
          { min: 10, message: '养护要求不能少于10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    generateApplyNo() {
      const date = new Date()
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const random = String(Math.floor(Math.random() * 1000)).padStart(3, '0')
      return `LH${year}${month}${day}${random}`
    },
    async submitForm() {
      try {
        await this.$refs.form.validate()
        this.submitting = true

        const [startTime, endTime] = this.formData.expectedTime
        const [area, section, spot] = this.formData.location

        const submitData = {
          ...this.formData,
          area,
          section,
          spot,
          expectedStartTime: startTime,
          expectedEndTime: endTime
        }
        delete submitData.location
        delete submitData.expectedTime

        await this.$http.post('/api/environment/green/submit', submitData)
        this.$message.success('绿化养护申请提交成功')
        this.$router.push('/main/huanjing/process')
      } catch (error) {
        if (error !== false) {
          const errorMsg = error.response?.data || '提交失败，请稍后重试'
          this.$message.error(errorMsg)
        }
      } finally {
        this.submitting = false
      }
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.formData.applyNo = this.generateApplyNo()
      this.formData.location = []
      this.formData.expectedTime = []
    }
  }
}
</script>

<style lang="scss" scoped>
.green-apply {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .application-form {
    max-width: 800px;
    margin: 0 auto;
  }
}
</style> 